package com.platform.qujia.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.platform.qujia.anno.SimpleRepeatedReqLimit;
import com.platform.qujia.constantEnum.RedisKey;
import com.platform.qujia.param.request.PayInfoRequest;
import com.platform.qujia.pojo.SaleActivityOrder;
import com.platform.qujia.pojo.req.SaleActivityOrderCreateReq;
import com.platform.qujia.service.ISaleActivityOrderService;
import com.platform.qujia.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @date 2023/12/25
 */
@RestController
@RequestMapping(value = "/saleActivityOrder")
public class SaleActivityOrderController {

    @Autowired
    private ISaleActivityOrderService saleActivityOrderService;

    @PostMapping("createOrder")
    @SimpleRepeatedReqLimit(name = RedisKey.SALE_ACTIVITY_ORDER_CREATE,express = "[0].getUserId()")
    public ResultData<Map<String,String>> createOrder(@RequestBody @Validated SaleActivityOrderCreateReq orderCreateReq){
        SaleActivityOrder activityOrder = saleActivityOrderService.createOder(orderCreateReq);
        return ResultData.success(Collections.singletonMap("orderNo",activityOrder.getOrderNo()));
    }

    @PostMapping("toThirdPay")
    @SimpleRepeatedReqLimit(name = RedisKey.SALE_ACTIVITY_ORDER_PAY,express = "[0].getOrdNo()",
            condition = "[0].getOrdNo() != null and [0].getOrdNo().length() > 0")
    public ResultData<Map<String,Object>> toThirdPay(@RequestBody PayInfoRequest payInfoRequest){
        return ResultData.success(saleActivityOrderService.toThirdPay(payInfoRequest));
    }

    @PostMapping("/callback/sxf")
    public String stkCallbackSxf(@RequestBody Map<String,Object> request){
        return saleActivityOrderService.callbackSxf(request);
    }

    @PostMapping("/callback/yh")
    public String stcCallbackYh(@RequestBody Map<String,Object> request){
        return saleActivityOrderService.callbackYh(request);
    }

    @GetMapping("list/user")
    public ResultData<PageInfo<SaleActivityOrder>> queryList(@RequestParam Long enterpriseId,
                                                             @RequestParam Integer userId,
                                                             @RequestParam String mobilePhone,
                                                             @RequestParam Integer pageNum,
                                                             @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SaleActivityOrder> list = saleActivityOrderService.queryList(enterpriseId, userId, mobilePhone);
        PageHelper.clearPage();
        return ResultData.success(new PageInfo<>(list));
    }

}
